include "UnitPanel_Expansion2"

include("Australia_UI_Utils");
include("Australia_UnitCommandDefs");

print("Australia_UnitPanel")

-- ===========================================================================
--	CACHE BASE FUNCTIONS
-- ===========================================================================
BASE_GetUnitActionsTable = GetUnitActionsTable;

-- ===========================================================================
--	OVERRIDE BASE FUNCTIONS
-- ===========================================================================
function GetUnitActionsTable(pUnit : object)
	local pBaseActionsTable : table = BASE_GetUnitActionsTable(pUnit);
  print("AUS GetUnitActionsTable Override")

	--	Unit Commands
	--	Test all custom commands in table defined in "Furude_Hanyuu" to add
	--	to the selected unit's table.
	for sCommandKey, pCommandTable in pairs(m_AUSUnitCommands) do
    local bVisible : boolean = true;
    if (pCommandTable.IsVisible ~= nil) then
      bVisible = pCommandTable.IsVisible(pUnit);
    end
    if (bVisible) then

      if (pCommandTable.CanUse ~= nil and pCommandTable.CanUse(pUnit) == true) then

        local bIsDisabled : boolean = false;
        if (pCommandTable.IsDisabled ~= nil) then
          bIsDisabled = pCommandTable.IsDisabled(pUnit);
        end
  
        local sToolTipString : string = pCommandTable.ToolTipString or "Undefined Unit Command";
        if pCommandTable.GetToolTipString ~= nil then
          local noErr, ttp = pcall(pCommandTable.GetToolTipString, pUnit);
          if noErr then
            sToolTipString = ttp;
          else
            print(ttp);
          end
        end

        local pCallback : ifunction = function()
          local pSelectedUnit = UI.GetHeadSelectedUnit();
          if (pSelectedUnit == nil) then
            return;
          end

          local tParameters = {};
          tParameters[UnitCommandTypes.PARAM_NAME] = pCommandTable.EventName or "";
          UnitManager.RequestCommand(pSelectedUnit, UnitCommandTypes.EXECUTE_SCRIPT, tParameters);
          if (pCommandTable.DoNotDelete == nil) or (pCommandTable.DoNotDelete ~= true) then
            UnitManager.RequestCommand(pSelectedUnit, UnitCommandTypes.DELETE);
          end
        end

        local sDisabledToolTipString : string = pCommandTable.DisabledToolTipString;
        if pCommandTable.GetDisabledToolTipString ~= nil then
          local noErr, ttp = pcall(pCommandTable.GetDisabledToolTipString, pUnit);
          if noErr then
            sDisabledToolTipString = ttp;
          else
            print(ttp);
          end
        end
        if (bIsDisabled and sDisabledToolTipString ~= nil) then
          sToolTipString = sToolTipString .. "[NEWLINE][NEWLINE]" .. sDisabledToolTipString;
        end

        AddActionToTable(pBaseActionsTable, pCommandTable, bIsDisabled, sToolTipString, UnitCommandTypes.EXECUTE_SCRIPT, pCallback);
      end
    end
  end

	return pBaseActionsTable;
end